package cn.lf.service.impl;

import cn.hutool.core.lang.UUID;
import cn.lf.constant.DelFlagTypeEnum;
import cn.lf.constant.ResponseCodeEnum;
import cn.lf.dto.producttype.*;
import cn.lf.entity.Product;
import cn.lf.entity.ProductType;
import cn.lf.entity.SysUser;
import cn.lf.exception.SelfException;
import cn.lf.mapper.ProductMapper;
import cn.lf.mapper.ProductTypeMapper;
import cn.lf.service.IProductTypeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品类型 服务实现类
 * </p>
 *
 * @author guoyy
 * @since 2024-04-16
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public void saveProductType(ProductTypeSaveReqDTO productTypeSaveReqDTO) {

        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();

        if (Objects.isNull(productTypeSaveReqDTO.getProductTypeId())||Objects.equals(productTypeSaveReqDTO.getProductTypeId(),"")){

            ProductType productType = new ProductType();
            BeanUtils.copyProperties(productTypeSaveReqDTO,productType);
            productType.setProductTypeId(UUID.randomUUID().toString());
            productType.setDelFlag(DelFlagTypeEnum.UN_DEL.getCode());

            productType.setCreateBy(user.getUserId());

            productType.setCreateTime(new Date());

            productType.setUpdateBy(user.getUserId());

            productType.setUpdateTime(new Date());

            baseMapper.insert(productType);

        }else {

            ProductType productType = baseMapper.selectById(productTypeSaveReqDTO.getProductTypeId());

            BeanUtils.copyProperties(productTypeSaveReqDTO,productType);

            productType.setUpdateBy(user.getUserId());

            productType.setUpdateTime(new Date());

            baseMapper.updateById(productType);

        }

    }

    @Override
    public ProductTypeSingleDTO getProductTypeById(String projectTypeId) {

        ProductTypeSingleDTO result = new ProductTypeSingleDTO();

        ProductType productType = baseMapper.selectById(projectTypeId);

        BeanUtils.copyProperties(productType,result);

        return result;
    }

    @Override
    public void delProductType(String projectTypeId) {

        List<Product> productList = productMapper.selectByProductTypeId(projectTypeId);

        if (!productList.isEmpty()){
            throw new SelfException(ResponseCodeEnum.SYS_ERROR.getCode(), "该类型下有商品信息，不允许删除");
        }

        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();

        ProductType productType = baseMapper.selectById(projectTypeId);

        productType.setDelFlag(DelFlagTypeEnum.HAVING_DEL.getCode());

        productType.setUpdateTime(new Date());

        productType.setUpdateBy(user.getUserId());

        baseMapper.updateById(productType);

    }

    @Override
    public List<ShopPtListDTO> getShopProductTypeList(String shopId) {
        return baseMapper.selectShopProductTypeList(shopId);
    }

    @Override
    public IPage<PtPageRepDTO> getProductTypePage(PtPageReqDTO params) {

        Page<PtPageRepDTO> page = new Page<>(params.getCurrentPage(),params.getPageSize());

        return baseMapper.selectProductTypePage(page,params);
    }
}
